﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
namespace University.Workshop.Model
{
    public class BankAccount : IDisposable
    {
        Client client = new Client();
        public string Parameter { get; set; }
        public BankAccount()
        { 
        }
        //correccion de warning
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
                client.Dispose();
            }
            // free native resources
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        public float Consult(string name, string cardNumber)
        {
            float resultEnd = 0;
            try
            {
                this.Parameter = "09 " + cardNumber + " " + name;
                client.Send(this.Parameter);
                resultEnd = Convert.ToInt64(client.Receive(), CultureInfo.InvariantCulture);
            }
            catch (ArgumentNullException)
            {

            }
            catch (ArgumentOutOfRangeException)
            {

            }
            catch (IOException)
            {

            }
            catch (ObjectDisposedException)
            {

            }
            return resultEnd;
        }
        public string ChangeAmount(string numberCard, string amount, string operation, string name)
        {
            string result = " ";
            try
            {
                this.Parameter = "40 " + numberCard + " " + amount + " " + operation + " " + name;
                client.Send(this.Parameter);
                result = client.Receive();
            }
            catch (ArgumentNullException)
            {

            }
            catch (ArgumentOutOfRangeException)
            {

            }
            catch (IOException)
            {

            }
            catch (ObjectDisposedException)
            {

            } 
            return result;
        }
        
        public string RegisterCreditCard(string name, string numberCard)
        {
            string result = "";
            try
            {
                this.Parameter = "41 " + name + " " + numberCard;
               client.Send(this.Parameter);
                result = client.Receive();//modificar un usuario para credit card
                
                
            }
            catch (ArgumentNullException)
            {

            }
            catch (ArgumentOutOfRangeException)
            {

            }
            catch (IOException)
            {

            }
            catch (ObjectDisposedException)
            {

            } return result;
        }

        public bool ConsultLogOn(string name, string numberCard)
        {
            bool result = false;
            this.Parameter = "42 " + numberCard + " " + name;
            client.Send(this.Parameter);

            string resultOfSend = client.Receive();
            if (resultOfSend == "yes")
                result = true;
            else
                result = false;
            return result;
        }
        /*public string logIn(string numCard, string name, string amount)
        {
            string result = " ";
            try
            {
                client.Send("41 " + numCard + " " + name + " " + amount);
                result = client.receive();//modificar un usuario para credit card
            }
            catch (Exception)
            {

            }
            return result;
        }*/
    }
}
